﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IForm_EditShortcuts</summary>
    public class IForm_EditShortcuts : IForm
    {
        public ICmd Value; // Objekt pre editaciu
        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_Default, FBtn_OK, FBtn_Cancel;
        protected IComboBox FCombo_Groups;
        protected IListView FList;

        /// <summary>Konstruktor</summary>
        public IForm_EditShortcuts()
        {
            Value = new ICmd();
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing">parameter</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing); // Volam predchodcu
            Value = (ICmd)ICore.DisposeObject(Value); // Zrusim objekt
        }

        /// <summary>Metoda OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowInTaskbar = false; MinimizeBox = MaximizeBox = false; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; // Inicializacia vlastnosti

            Text = lang.FORM_EDIT_SHORTCUTS_TEXT_4;
            SetBounds(0, 0, 700, 500); System.Drawing.Size CS = ClientSize;

            FGrp = new IGroupBox(); // Vytvorim groupbox
            FBtn_Default = new IButton(lang.FORM_EDIT_SHORTCUTS_TEXT_1, Event_DefaultBtnClick);
            FBtn_OK = new IButton(lang.FORM_EDIT_SHORTCUTS_TEXT_2);
            FBtn_Cancel = new IButton(lang.FORM_EDIT_SHORTCUTS_TEXT_3);

            FCombo_Groups = new IComboBox(); // Vytvorim combobox
            for (int i = 0; i < ICmd.CMD_GROUP_LAST; i++) FCombo_Groups.AddItem(ICmd.GroupID2Str(i), i);

            FList = new IListView(); FList.HideSelection = false; FList.FullRowSelect = true; FList.ShowItemToolTips = true; // Vytvorim listview
            FList.CreateColumns(new string[] { lang.FORM_EDIT_SHORTCUTS_TEXT_5_1, lang.FORM_EDIT_SHORTCUTS_TEXT_5_2, lang.FORM_EDIT_SHORTCUTS_TEXT_5_3 }, new int[] { 260, 190, 190 }, new System.Windows.Forms.HorizontalAlignment[] { System.Windows.Forms.HorizontalAlignment.Left, System.Windows.Forms.HorizontalAlignment.Center, System.Windows.Forms.HorizontalAlignment.Center });

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FCombo_Groups, FList }); // Zaradim
            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_Default, FBtn_OK, FBtn_Cancel }); // Zaradim

            IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50); // Umiestnim prvky do okna
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FBtn_Default, FGrp, 0, 10);
            IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10);
            IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);
            IControl.PlaceControl_Inside_LeftTop_LeftTop(FCombo_Groups, FGrp, 4, 10); FCombo_Groups.Width = FGrp.ClientSize.Width - 8;
            FList.Width = FCombo_Groups.Width; IControl.PlaceControl_Outside_LeftTop_LeftBottom(FList, FCombo_Groups, 0, 0); FList.Height = FGrp.ClientSize.Height - FCombo_Groups.Bottom - 8;

            AcceptButton = FBtn_OK; CancelButton = FBtn_Cancel; FBtn_OK.Focus(); FBtn_OK.DialogResult = System.Windows.Forms.DialogResult.OK;

            // Nastavim eventy
            FCombo_Groups.SelectedIndexChanged += Event_SelectedIndexChanged;
            FList.ItemActivate += Event_ItemActivate;

            CreateList(ICmd.CMD_GROUP_COMMON);
            return true; // Vsetko OK
        }
        /// <summary>Vytvorenie zoznamu podla zadanej skupiny</summary>
        /// <param name="pGroupID">ID skupiny</param>
        protected void CreateList(int pGroupID)
        {
            FCombo_Groups.SelectedIntID = pGroupID; // Nastavim skupinu v comboboxe

            FList.BeginCreate(); // Zacinam vytvarat obsah

            for (int i = 1; i < ICmd.CMD_ID_LAST; i++) // Prejdem vsetky funkcie
            {
                if (!ICmd.CanHasShortcut(i)) continue;
                if (ICmd.ID2GroupID(i) != pGroupID) continue;
                FList.AddItem(ICmd.SubgroupID2Str(ICmd.ID2SubroupID(i)), ICmd.ID2Str(i), i);
            }

            UpdateList(); // Doplnim hodnoty
            FList.EndCreate(-1); // Koniec vytvarania
        }
        /// <summary>Aktualizacia zoznamu</summary>
        protected void UpdateList()
        {
            foreach (IListViewItem LI in FList.Items) // Prejdem zaznamy
            {
                LI[1] = ICmd.Key2Str(Value.GetShortcut(LI.IntTag, false));
                LI[2] = ICmd.Key2Str(Value.GetShortcut(LI.IntTag, true));
            }
        }
        /// <summary>Zmena klav. skratky pomocou formulara</summary>
        /// <param name="pID">ID funkcie</param>
        /// <returns>true / false</returns>
        protected bool EditValue(int pID)
        {
            int R = 0;
            if (!IApp.ShowEditor_ChooseFromList(Text, lang.FORM_EDIT_SHORTCUTS_TEXT_6_1, lang.FORM_EDIT_SHORTCUTS_TEXT_6_2, lang.FORM_EDIT_SHORTCUTS_TEXT_6_3, ref R)) return false; // Zistim, ktoru klavesu mam menit
            bool Alt = R == 1; // Ziskam priznak alternativnej klavesy

            int OldK = Value.GetShortcut(pID, Alt); // Ziskam aktualnu skratku
            int GrpID = ICmd.ID2GroupID(pID); // Ziskam ID skupiny

            int K = OldK;
            while (true)
            {
                if (!ICmd.EditShortcut(Alt ? lang.FORM_EDIT_SHORTCUTS_TEXT_6_5 : lang.FORM_EDIT_SHORTCUTS_TEXT_6_4, ref K)) return false; // Volam editaciu skratky
                if (K == OldK) return false; // Ziadna zmena?

                if (K == ICmd.KEY_NONE) // Rusim skratku?
                {
                    Value.SetShortcut(pID, Alt, K); // Nastavim
                    return true; // Aktualizujem zoznam
                }

                int ID1 = Value.FindCMD(K, GrpID); // Hladam ci je skratka pouzita v skupine
                if (ID1 == ICmd.CMD_ID_NONE) // Nie je...
                {
                    Value.SetShortcut(pID, Alt, K); // Nastavim
                    return true; // Aktualizujem zoznam
                }
                if (ID1 == pID) // Je priradena tomu istemu prikazu?
                {
                    Value.SetShortcut(pID, !Alt, ICmd.KEY_NONE);
                    Value.SetShortcut(pID, Alt, K); // Nastavim
                    return true; // Aktualizujem zoznam
                }

                R = 0;
                if (!IApp.ShowEditor_ChooseFromList(Text, string.Format(lang.FORM_EDIT_SHORTCUTS_TEXT_7_1, ICmd.Key2Str(K), ICmd.ID2Str(ID1)), lang.FORM_EDIT_SHORTCUTS_TEXT_7_2, lang.FORM_EDIT_SHORTCUTS_TEXT_7_3, ref R)) return false; // Zistim, co robit
                if (R == 0) // Nastavit nove priradenie?
                {
                    int K1 = Value.GetShortcut(ID1, true);
                    Value.SetShortcut(ID1, K1 == K, ICmd.KEY_NONE); // Zrusim stare priradenie
                    Value.SetShortcut(pID, Alt, K); // Nastavim
                    return true; // Aktualizujem zoznam
                }
            }
        }

        /// <summary>Event: SelectedIndexChanged</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_SelectedIndexChanged(object pSender, EventArgs e)
        {
            CreateList(FCombo_Groups.SelectedIntID); // Volam zmenu skupiny
        }
        /// <summary>Event: ItemActivate</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_ItemActivate(object pSender, EventArgs e)
        {
            int ID = FList.SelectedIntTag; // Ziskam ID funkcie
            if (ID == -1) return; // Ziadna?
            if (EditValue(ID)) UpdateList(); // Volam zmenu skratky a aktualizaciu zoznamu
        }
        /// <summary>Event: Click pre tlacidlo 'Prednastavene'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_DefaultBtnClick(object pSender, EventArgs e)
        {
            if (IApp.ShowQuestion(lang.FORM_EDIT_SHORTCUTS_TEXT_8)) // Potvrdim
            {
                Value.LoadDefaults(); // Nacitam prednastavene
                UpdateList(); // Aktualizujem zoznam
            }
        }
    }
}
